<%@ page import="com.wuming.house.security.SecurityHander" %>
<%--
  Created by IntelliJ IDEA.
  User: zengyao
  Date: 2018/6/29
  Time: 17:18
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>用户管理</title>
    <link rel="stylesheet" href="${allServerPath}static/layui/layui/css/layui.css">
    <style>
        .query-condition {
            margin-top: 15px;
        }
    </style>
</head>
<body style="background-color: #f0f2f5;">
<div style="padding: 15px;margin: 15px;background-color: #fff;">
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>用户管理</legend>
    </fieldset>
    <form id="queryForm" class="layui-form" action="">
        <div class="layui-form-item query-condition">

            <div class="layui-inline">
                <label class="layui-form-label">用户名</label>

                <div class="layui-input-inline">
                    <input name="userName" class="layui-input" type="text" placeholder="请输入用户名" autocomplete="off">
                </div>
            </div>
        </div>
        <div class="layui-form-item query-condition">
            <div class="layui-inline">
                <label class="layui-form-label">手机号码</label>

                <div class="layui-input-inline">
                    <input name="phone" class="layui-input" type="tel" autocomplete="off" placeholder="请输入手机号码">
                </div>
            </div>
        </div>
    </form>
    <div class="layui-row" style="padding: 9px 15px;margin:15px 0 10px 0;">
        <button name="reQuery" class="layui-btn layui-btn-normal">查询搜索</button>
        <button name="export" class="layui-btn layui-btn-normal">导出表格</button>
    </div>
    <table id="userInfo" lay-filter="test" lay-data="{height: 'full-200'}"></table>
</div>
<script type="text/html" id="barDemo">
    <%--<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">编辑</a>--%>
    <%--<a class="layui-btn layui-btn-xs" lay-event="del">删除</a>--%>
</script>

<script type="text/html" id="genderTpl">
    {{#  if(d.gender == '1'){ }}
    <span>男</span>
    {{#  } else { }}
    <span>女</span>
    {{#  } }}
</script>


<script src="${allServerPath}static/layui/layui/layui.js"></script>
<script>
    layui.config({
        base: '${allServerPath}static/layui/build/js/'
    }).use(['table', 'jquery', 'navbar'], function () {
        var table = layui.table,
            $ = layui.jquery;

        $('button[name="export"]').on('click', function () {
            $.post('${allServerPath}/web/user/export.htmls',
                function (data) {
                    if (data.success) {
                        layer.msg('导出成功.');
                        FileUtil.downFile("${allServerPath}/web/user/downLoadTempFile.htmls", data.data);
                    } else {
                        layer.msg('导出失败,请稍后重试.');
                    }
                }, "json");
        });

        table.render({
            elem: '#userInfo'
//            , height: 315
            , url: '${allServerPath}web/user/list.htmls?t=' + Date.parse(new Date()) //数据接口
            , page: true //开启分页
            , cols: [[ //表头
                {field: 'id', title: 'ID', width: 80, fixed: 'left'}
                , {field: 'nickname', title: '昵称', align: 'center', width: 100}
                , {field: 'realname', title: '用户名', align: 'center', width: 100}
                , {field: 'province', title: '省', align: 'center', width: 100}
                , {field: 'city', title: '市', align: 'center', width: 100}
                // , {field: 'userAddr', title: '地址', align: 'center', width: 200}
                , {field: 'gender', title: '性别', align: 'center', width: 100, templet: '#genderTpl'}
                , {field: 'mobile', title: '手机号', align: 'center', width: 140}
                , {field: 'age', title: '年龄', align: 'center', width: 100}
                // , {field: 'profession', title: '职业', align: 'center', width: 100}
                , {fixed: 'right', title: '操作', width: 260, align: 'center', toolbar: '#barDemo'}
            ]],
            id: 'userContent',
            method: 'get',
            page: true,
            request: {
                pageName: 'pageIndex', //页码的参数名称，默认：page
                limitName: 'pageSize'
            }, //如果无需自定义请求参数，可不加该参数
            response: {
                statusName: 'code' //数据状态的字段名称，默认：code
                , statusCode: 200 //成功的状态码，默认：0
                , msgName: 'message' //状态信息的字段名称，默认：msg
                , countName: 'total' //数据总数的字段名称，默认：count
                , dataName: 'data' //数据列表的字段名称，默认：data
            },
            done: function (res, curr, count) {
                //如果是异步请求数据方式，res即为你接口返回的信息。
                //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
                console.log(res);
                //得到当前页码
                console.log(curr);
                //得到数据总量
                console.log(count);
            },
            text: {
                none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
            }

        });
        table.on('tool(test)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'edit') {
                layer.open({
                    type: 2,
                    title: '查看用户信息',
                    maxmin: true,
                    shadeClose: true, //点击遮罩关闭层
                    area: ['725px', '460px'],
                    content: '${allServerPath}web/member/editUser/' + data.id + '.htmls'
                });
            } else if (layEvent === 'del') {
                layer.confirm('真的删除么', function (index) {
                    $.ajax({
                        url: '${allServerPath}web/member/delete/' + data.id + ".htmls",
                        method: 'get',
                        dataType: 'json',
                        success: function (result) {
                            if (result.success) {
                                obj.del(); //删除对应行（tr）的DOM结构
                                layer.close(index);//向服务端发送删除指令
                            }
                        },
                        error: function () {
                            layer.msg('删除失败,请稍后重试.');
                        }
                    });
                });
            }
        });

        $("button[name='reQuery']").click(function () {
            var realname = $('input[name="userName"]').val();
            var mobile = $('input[name="phone"]').val();
            table.reload('userContent', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                , where: {
                    "name": realname,
                    "mobile": mobile
                }
            });

        });

        /**
         * 下载文件链接
         */
        var FileUtil = {
            downFile: function (url, data) {
                if (url && data) {
                    var inputs = '';
                    for (var p in data) {
                        inputs += '<input type="hidden" name="' + p + '" value="' + data[p]
                            + '" />';
                    }
                    $('<form action="' + url + '" method="post">' + inputs + '</form>')
                        .appendTo('body').submit().remove();
                }
            }
        }
    });
</script>
</body>
</html>
